﻿using Sayook.Framework.DbCore.DbException;

namespace Sayook.Framework.DbCore.SqlModel
{
    /// <summary>
    /// SQL语句模型
    /// </summary>
    public class SqlString
    {
        private string select;

        /// <summary>
        /// SELECT 字句
        /// </summary>
        public string Select
        {
            get
            {
                return select;
            }
            set
            {
                if (!string.IsNullOrWhiteSpace(Insert) ||
                    !string.IsNullOrWhiteSpace(Update) ||
                    !string.IsNullOrWhiteSpace(Delete))
                {
                    throw new CreateSqlException("每次操作只能执行 增|改|查|删 中一个命令！");
                }
                select = value;
            }
        }

        private string insert;

        /// <summary>
        /// INSERT 字句
        /// </summary>
        public string Insert
        {
            get
            {
                return insert;
            }
            set
            {
                if (!string.IsNullOrWhiteSpace(Select) ||
                    !string.IsNullOrWhiteSpace(Update) ||
                    !string.IsNullOrWhiteSpace(Delete))
                {
                    throw new CreateSqlException("每次操作只能执行 增|改|查|删 中一个命令！");
                }
                insert = value;
            }
        }

        private string update;

        /// <summary>
        /// UPDATE 字句
        /// </summary>
        public string Update
        {
            get
            {
                return update;
            }
            set
            {
                if (!string.IsNullOrWhiteSpace(Select) ||
                    !string.IsNullOrWhiteSpace(Insert) ||
                    !string.IsNullOrWhiteSpace(Delete))
                {
                    throw new CreateSqlException("每次操作只能执行 增|改|查|删 中一个命令！");
                }
                update = value;
            }
        }

        private string delete;

        /// <summary>
        /// DELETE 字句
        /// </summary>
        public string Delete
        {
            get
            {
                return delete;
            }
            set
            {
                if (!string.IsNullOrWhiteSpace(Select) ||
                    !string.IsNullOrWhiteSpace(Insert) ||
                    !string.IsNullOrWhiteSpace(Update))
                {
                    throw new CreateSqlException("每次操作只能执行 增|改|查|删 中一个命令！");
                }
                delete = value;
            }
        }

        /// <summary>
        /// WHERE 字句
        /// </summary>
        public string Where { get; set; }

        /// <summary>
        /// ORDER BY 字句
        /// </summary>
        public string OrderBy { get; set; }

        /// <summary>
        /// 分页字句
        /// </summary>
        public string Paging { get; set; }

        private int skipCount;

        /// <summary>
        /// 跳过的数量 默认0行
        /// </summary>
        public int SkipCount
        {
            get
            {
                if (skipCount < 0)
                {
                    skipCount = 0;
                }
                return skipCount;
            }
            set
            {
                skipCount = value;
            }
        }

        private int takeCount;

        /// <summary>
        /// 查询的数量 默认100行
        /// </summary>
        public int TakeCount
        {
            get
            {
                if (takeCount <= 0)
                {
                    takeCount = 100;
                }
                return takeCount;
            }
            set
            {
                takeCount = value;
            }
        }

        /// <summary>
        /// 完整的 SELECT 语句
        /// </summary>
        /// <returns></returns>
        public string QuerySql()
        {
            return Select + Where + OrderBy + Paging;
        }

        /// <summary>
        /// 完整的 UPDATE 语句
        /// </summary>
        /// <returns></returns>
        public string UpdateSql()
        {
            return string.IsNullOrWhiteSpace(Update) ? null : Update + Where;
        }

        /// <summary>
        /// 完整的 DELETE 语句
        /// </summary>
        /// <returns></returns>
        public string DeleteSql()
        {
            return string.IsNullOrWhiteSpace(Delete) ? null : Delete + Where;
        }
    }
}